<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML" 4.01"//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!-- Created: May 28, 2002-->
<!--
$Id$
$Header$
$Log$
-->

<html>
<head>
<title>Radiological Equipment Database</title>
<link rev="made" href="mailto:eugenem@ix.netcom.com">
<link rev="home" href="http://home.netcom.com/~eugenem/">
<meta name="author" content="Eugene Mah">
<meta name="description" content="">
<meta name="Keywords" content="">
<meta name="Reply-to" content="eugenem@ix.netcom.com">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<META http-equiv="PICS-Label" content='(PICS-1.1 "http://www.classify.org/safesurf/" l r (SS~~0001))'>
<?PHP
require('functions.inc');
require('dbc.inc');

/*
  Define constants that indicate what each line is used for
*/
define("LINEARITY", 0x1);  // Low bit
define("ACCURACY", 0x2);
define("BEAMQUAL", 0x4);
define("REPRO", 0x8);  // High bit
?>
</head>

<body>
<h1>Radiological Equipment Database</h1>
<!--
This form should never be entered directly.  All input to this
form should come from SurveyGenDataA.php.
-->
<?PHP
if (isset($SurveyID) && isset($MachineID) && isset($TubeID)) {
  /*
    Verify that generator data from $SurveyID doesn't already exist
    in the DB
  */
  if (mysql_num_rows(mysql_query("select GenID from GenData where SurveyID=$SurveyID and TubeID=$TubeID")) > 0) {
    echo "<h2>Data already exists</h2>\n";
    echo "<p>Generator data already exists for Survey $SurveyID.\n</p>";
    exit();
  }

  /*
    Convert the contents of $GenData containing the pasted generator data
    into an array.
  */
  $gdrows = explode("\n",$GenData);
  $n = count($gdrows);
  $useflag = 0;
  for ($i=0;$i<$n-1;$i++) {
    $query = "INSERT INTO GenData (SurveyID,TubeID,kVset,mAset,Timeset,";
    $query .= "mAsset,AddFilt,Distance,UseFlags,kVAvg,kVMax,kVEff,ExpTime,Exp) ";
    $query .= "values (\"$SurveyID\",\"$TubeID\",";
    /*
      Split each row into an array with each element
      containing a column of data after rtrim()ming any trailing spaces
      or newlines.
    */
    $gdcol = explode("\t",rtrim($gdrows[$i]));
    /*
      Only columns 1-4,6,8,17-19,21,24-28 are used.
      Columns 1-4,6,8 are the set values
    */
    $query .= "$gdcol[0],$gdcol[1],$gdcol[2],$gdcol[3],$gdcol[5],$gdcol[7],";

    /*
      Take the linearity, accuracy, beam quality and reproducibility flags
      from the table and pack it all into one byte
      bit 0 - linearity
      bit 1 - accuracy
      bit 2 - beam quality
      bit 3 - reproducibility

      Columns 17-19,21 contain 1 if the current row is used for that
      particular measurement, and 0 if it isn't.
    */
    $useflag = ($gdcol[16] << 0) | ($gdcol[17] << 1) | ($gdcol[18] << 2) | ($gdcol[20] << 3);

    $query .= "$useflag,";

    /*
      Columns 24-28 contain the actual measurements.
      If there is no value, then replace it with a 0
    */
    for ($j=23;$j<=26;$j++) {
      if (empty($gdcol[$j])) { $query .= "0,"; }
      else { $query .= "$gdcol[$j],"; }
    }

    /*
      The last array element has a newline tacked onto it.
      Use trim() to remove it, and tack on a trailing 0 in case
      $gdcol[27] is empty after trim()ming..
    */
    if (empty($gdcol[27])) { $query .= "0)"; }
    else { $query .= "$gdcol[27])"; }

    $result = mysql_query($query)
      or die("<p>Unable to execute query: $query<br />" . mysql_error() . "</p>");
  }
  echo "<h2>Generator Data Added</h2>\n";
  echo "<p>Generator data added for Survey $SurveyID</p>\n";

}
?>
<p><a href="index.html" target="_top" >Return to main menu</a></p>
</body>
</html>
